/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xstgongyi.eduaid.common.ControllerHelper;
import org.xstgongyi.eduaid.dataobject.VolunteerDO;
import org.xstgongyi.eduaid.service.VolunteerService;

/**
 * @author oldcrane<br/>
 * @created 2019-04-17
 */
@Controller
@RequestMapping("/volunteers")
public class VolunteerController {

	@Autowired
	private VolunteerService volunteerService;

	@GetMapping("/entry")
	public String entryPage(ModelMap map) {
		return "aid/volunteer_list";
	}


	@GetMapping("")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "page", defaultValue = "1") int pageNumber,
			@RequestParam(name = "limit", defaultValue = "20") int pageSize) {
		Map<String, Object> result = new HashMap<>();
		List<VolunteerDO> rows = new ArrayList<>(); // 如果没有记录，返回空数组，而非null
		keyword = null == keyword ? "" : keyword.trim();
		int total = volunteerService.countTotal(keyword);
		if (total > 0) {
			rows = volunteerService.listPage(keyword, pageNumber, pageSize);
		}
		result.put("page", pageNumber);
		result.put("total", total);
		result.put("rows", rows);
		return ControllerHelper.successResponse(result);
	}

	@GetMapping("/list_for_selection")
	@ResponseBody
	public Map<String, Object> list_for_selection(@RequestParam(name = "page", defaultValue = "1") int pageNumber,
												  @RequestParam(name = "limit", defaultValue = "9999") int pageSize) {
		List<VolunteerDO> volunteerList = volunteerService.listPage(null, pageNumber, pageSize);

		List<Map<String, String>> result = new ArrayList<>();
		for (VolunteerDO volunteer : volunteerList) {
			Map<String, String> row = new HashMap<String, String>();
			row.put("id", String.valueOf(volunteer.getId()));
			if (null != volunteer.getNickname() && volunteer.getNickname().length() > 0) {
				row.put("name", volunteer.getName() + "(" + volunteer.getNickname() + ")");
			} else {
				row.put("name", volunteer.getName());
			}
			result.add(row);
		}
		return ControllerHelper.successResponse(result);

	}


		@PostMapping("")
	@ResponseBody
	public Map<String, Object> addVolunteer(@RequestBody VolunteerDO volunteer) {
		volunteerService.insert(volunteer);
		return ControllerHelper.successResponse(null);
	}

	@GetMapping("/{id}")
	@ResponseBody
	public Map<String, Object> getVolunteer(@PathVariable("id") Integer id) {
		VolunteerDO volunteer = volunteerService.getById(id);
		if (null == volunteer) {
			return ControllerHelper.errorResponse(404, "volunteer(id=" + id + ") not exist.");
		} else {
			return ControllerHelper.successResponse(volunteer);
		}
	}

	@PostMapping("/{id}")
	@ResponseBody
	public Map<String, Object> updateVolunteer(@PathVariable("id") Integer id, @RequestBody VolunteerDO inputObj) {
		VolunteerDO existVolunteer = volunteerService.getById(id);
		VolunteerDO volunteer = VolunteerControllerHelper.merge(existVolunteer, inputObj);
		volunteerService.update(volunteer);
		return ControllerHelper.successResponse(null);
	}

}
